<?php

class DB 
{
	public $query_num = 0;
	public $link;
	public $histories;
	public $time;
	public $tablepre;
	public $cache;

	function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset = '', $pconnect = 0, $tablepre='', $time = 0) {
		$this->time = $time;
		$this->tablepre = $tablepre;
		if($pconnect) {
			if(!$this->link = mysql_pconnect($dbhost, $dbuser, $dbpw)) {
				$this->halt('Can not connect to MySQL server');
			}
		} else {
			if(!$this->link = mysql_connect($dbhost, $dbuser, $dbpw, 1)) {
				$this->halt('Can not connect to MySQL server');
			}
		}

		if($this->version() > '4.1') {

			if($dbcharset) {
				mysql_query("SET character_set_connection=".$dbcharset.", character_set_results=".$dbcharset.", character_set_client=binary", $this->link);
			}

			if($this->version() > '5.0.1') {
				mysql_query("SET sql_mode=''", $this->link);
			}
		}

		if($dbname) {
			mysql_select_db($dbname, $this->link);
		}
	}

	function fetchArray($query, $result_type = MYSQL_ASSOC) {
		return mysql_fetch_array($query, $result_type);
	}

	function resultFirst($sql) {
		$query = $this->query($sql);
		return $this->result($query, 0);
	}

	function fetchOne($sql) {
		$query = $this->query($sql);
		return $this->fetchArray($query);
	}
	
	function fetchAll($sql) {
		$arr = array();
		$query = $this->query($sql);
		while( $data = $this->fetchArray($query) ) {
			$arr[] = $data;
		}
		return $arr;
	}

	function fetchFields($query) {
		return mysql_fetch_field($query);
	}

	function affectedRows() {
		return mysql_affected_rows($this->link);
	}

	function result($query, $row) {
		$query = @mysql_result($query, $row);
		return $query;
	}

	function numRows($query) {
		$query = mysql_num_rows($query);
		return $query;
	}

	function numFields($query) {
		return mysql_num_fields($query);
	}
	
	function insertId() {
		return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
	}

	function version() {
		return mysql_get_server_info($this->link);
	}

	function close() {
		return mysql_close($this->link);
	}
	
	function error() {
		return (($this->link) ? mysql_error($this->link) : mysql_error());
	}

	function errno() {
		return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
	}
	function halt($message = '', $sql = '') {
		exit($message.'<br /><br />'.$sql.'<br /> '.mysql_error());
	}
	
	/**
	* Base query method
	*
	* @param	string	$query		Contains the SQL query which shall be executed
	* @param	int		$cache_ttl	Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
	* @return	mixed				When casted to bool the returned value returns true on success and false on failure
	*
	* @access	public
	*/
	function query($query = '', $cache_ttl = 0)
	{
		if ($query != '') {
			// EXPLAIN only in extra debug mode
			$this->query_result = ($cache_ttl && method_exists($this->cache, 'sqlLoad')) ? $this->cache->sqlLoad($query) : false;
			if ($this->query_result === false) {
				if (($this->query_result = @mysql_query($query, $this->link)) === false) {
					$this->halt('MySQL Query Error', $query);
				}
				if ($cache_ttl && method_exists($this->cache, 'sqlSave')) {
					$this->open_queries[(int) $this->query_result] = $this->query_result;
					$this->cache->sqlSave($query, $this->query_result, $cache_ttl);
				} else if (strpos($query, 'SELECT') === 0 && $this->query_result) {
					$this->open_queries[(int) $this->query_result] = $this->query_result;
				}
				$this->query_num++;
				$this->histories[] = $query;
			}
		} else {
			return false;
		}
		return ($this->query_result) ? $this->query_result : false;
	}	

	/**
	* Fetch current row
	*/
	function fetchRow($query_id = false)
	{
		if ($query_id === false) {
			$query_id = $this->query_result;
		}

		if (isset($this->cache->sql_rowset[$query_id])) {
			return $this->cache->sqlFetchRow($query_id);
		}

		return ($query_id !== false) ? @mysql_fetch_assoc($query_id) : false;
	}

	/**
	* Seek to given row number
	* rownum is zero-based
	*/
	function rowSeek($rownum, &$query_id)
	{
		if ($query_id === false) {
			$query_id = $this->query_result;
		}

		if (isset($this->cache->sql_rowset[$query_id])) {
			return $this->cache->sqlRowSeek($rownum, $query_id);
		}

		return ($query_id !== false) ? @mysql_data_seek($query_id, $rownum) : false;
	}
	
	/**
	* Free sql result
	*/
	function freeResult($query_id = false)
	{
		if ($query_id === false) {
			$query_id = $this->query_result;
		}

		if (isset($this->cache->sql_rowset[$query_id])) {
			return $this->cache->sqlFreeResult($query_id);
		}

		if (isset($this->open_queries[(int) $query_id])) {
			unset($this->open_queries[(int) $query_id]);
			return @mysql_free_result($query_id);
		}

		return false;
	}		
}

?>